<!DOCTYPE html>

<html>
<head>
  <title>auditData.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div class="container">
    <div class="page">

      <div class="header">
        
          <h1>auditData.coffee</h1>
        

        
          <div class="toc">
            <h3>Table of Contents</h3>
            <ol>
              
                
                <li>
                  <a class="source" href="admin.html">
                    admin.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.auditData.html">
                    api.auditData.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.auditEvent.html">
                    api.auditEvent.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.html">
                    api.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.entity.html">
                    api.entity.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.map.html">
                    api.map.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.server.html">
                    api.server.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.variable.html">
                    api.variable.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.view.html">
                    api.view.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="app.instance.html">
                    app.instance.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="appjs.html">
                    appjs.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="data.html">
                    data.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="dataUtil.html">
                    dataUtil.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="manager.html">
                    manager.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="messages.html">
                    messages.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditData.html">
                    auditData.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditEvent.html">
                    auditEvent.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="base.html">
                    base.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityDefinition.html">
                    entityDefinition.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityObject.html">
                    entityObject.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="eventAction.html">
                    eventAction.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="eventRule.html">
                    eventRule.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="link.html">
                    link.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="map.html">
                    map.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shape.html">
                    shape.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="user.html">
                    user.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="userSettings.html">
                    userSettings.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="variable.html">
                    variable.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="adminRoutes.html">
                    adminRoutes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="appRoutes.html">
                    appRoutes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="settings.html">
                    settings.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sockets.html">
                    sockets.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="tutorial.html">
                    tutorial.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="vectors.html">
                    vectors.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="statusTabView.html">
                    statusTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="toolsTabView.html">
                    toolsTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="usersTabView.html">
                    usersTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="adminView.html">
                    adminView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="alertView.html">
                    alertView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditDataManagerView.html">
                    auditDataManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditEventManagerView.html">
                    auditEventManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="baseView.html">
                    baseView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="createMapView.html">
                    createMapView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityManagerView.html">
                    entityManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="footerView.html">
                    footerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="helpView.html">
                    helpView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entitiesTabView.html">
                    entitiesTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="inspectorTabView.html">
                    inspectorTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="mapTabView.html">
                    mapTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeTabView.html">
                    shapeTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="controlsView.html">
                    controlsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="labelEditView.html">
                    labelEditView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkCreatorView.html">
                    linkCreatorView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkLabelsView.html">
                    linkLabelsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkView.html">
                    linkView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeLabelsView.html">
                    shapeLabelsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeView.html">
                    shapeView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapesMoverView.html">
                    shapesMoverView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="mapView.html">
                    mapView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="menuView.html">
                    menuView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="overlayView.html">
                    overlayView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="scriptEditorView.html">
                    scriptEditorView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="settingsView.html">
                    settingsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="startView.html">
                    startView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="variableManagerView.html">
                    variableManagerView.coffee
                  </a>
                </li>
              
            </ol>
          </div>
        
      </div>

      
        
        <h2 id="audit-data-model">AUDIT DATA MODEL</h2>

        
      
        
        <p>Represents audit data from a single or multiple machines / hosts / services.</p>

        
          <div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">SystemApp</span>.<span class="title">AuditData</span> <span class="keyword">extends</span> <span class="title">SystemApp</span>.<span class="title">BaseModel</span></span>
    <span class="attribute">typeName</span>: <span class="string">"AuditData"</span>
    <span class="attribute">defaults</span>:
        <span class="attribute">refreshInterval</span>: SystemApp.Settings.auditData.refreshInterval</pre></div>
        
      
        
        <p>Holds the refresh error count. We&#39;ll use this to alert the user in case the audit data
couldn&#39;t be refreshed for more than X times - defined at the <a href="settings.html">Settings</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">refreshErrorCount</span>: <span class="number">0</span></pre></div>
        
      
        
        <p>Holds the time when the property <code>data</code> has been last refreshed and saved to the DB server.</p>

        
          <div class='highlight'><pre>    <span class="attribute">lastDataRefresh</span>: <span class="keyword">new</span> Date()
    <span class="attribute">lastDataSave</span>: <span class="keyword">new</span> Date(<span class="number">2000</span>, <span class="number">1</span>, <span class="number">1</span>)</pre></div>
        
      
        
        <h2 id="properties">PROPERTIES</h2>

        
      
        
        <p>Helper to get / set the audit data values.</p>

        
          <div class='highlight'><pre>    <span class="attribute">data</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        <span class="keyword">if</span> value?
            <span class="property">@set</span> <span class="string">"data"</span>, value
        <span class="property">@get</span> <span class="string">"data"</span></pre></div>
        
      
        
        <p>Helper to get / set the audit data description.</p>

        
          <div class='highlight'><pre>    <span class="attribute">description</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        <span class="keyword">if</span> value?
            <span class="property">@set</span> <span class="string">"description"</span>, value
        <span class="property">@get</span> <span class="string">"description"</span></pre></div>
        
      
        
        <p>Helper to get / set the audit data notes.</p>

        
          <div class='highlight'><pre>    <span class="attribute">notes</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        <span class="keyword">if</span> value?
            <span class="property">@set</span> <span class="string">"notes"</span>, value
        <span class="property">@get</span> <span class="string">"notes"</span></pre></div>
        
      
        
        <p>Helper to get / set the audit data refresh interval.</p>

        
          <div class='highlight'><pre>    <span class="attribute">refreshInterval</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        <span class="keyword">if</span> value?
            <span class="property">@set</span> <span class="string">"refreshInterval"</span>, value
        <span class="property">@get</span> <span class="string">"refreshInterval"</span></pre></div>
        
      
        
        <p>Helper to get / set the original source URL.</p>

        
          <div class='highlight'><pre>    <span class="attribute">sourceUrl</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        <span class="keyword">if</span> value?
            <span class="property">@set</span> <span class="string">"sourceUrl"</span>, value
        <span class="property">@get</span> <span class="string">"sourceUrl"</span></pre></div>
        
      
        
        <h2 id="validate">VALIDATE</h2>

        
      
        
        <p>All validation rules should be put inside this method.</p>

        
          <div class='highlight'><pre>    <span class="attribute">validate</span>: <span class="function"><span class="params">(attrs)</span> =&gt;</span></pre></div>
        
      
        
        <p>Refresh interval can&#39;t be too low.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> attrs.refreshInterval &lt; SystemApp.Settings.auditData.minRefreshInterval
            <span class="keyword">return</span> SystemApp.Messages.valRefreshIntervalTooLow</pre></div>
        
      
        
        <h2 id="methods">METHODS</h2>

        
      
        
        <p>Refresh the <code>data</code> property from the model&#39;s specified <code>sourceUrl</code> This will use the
server&#39;s proxy downloader (see the <a href="server/routes.html">Server Routes</a> for more info)
to avoid cross-domain issues.</p>

        
          <div class='highlight'><pre>    <span class="attribute">refreshData</span>:<span class="function"> =&gt;</span>
        <span class="keyword">if</span> <span class="property">@fetching</span>
            <span class="keyword">return</span>

        <span class="property">@fetching</span> = <span class="literal">true</span>

        $.ajax
            <span class="attribute">url</span>: SystemApp.Settings.general.remoteDownloaderUrl + <span class="string">"auditdata-"</span> + <span class="property">@id</span>
            <span class="attribute">timeout</span>: <span class="property">@refreshInterval</span>() * <span class="number">3</span>
            <span class="attribute">cache</span>: <span class="literal">false</span>
            <span class="attribute">dataType</span>: <span class="string">"json"</span>
            <span class="attribute">type</span>: <span class="string">"POST"</span>
            <span class="attribute">success</span>: <span class="property">@dataRefreshSuccess</span>
            <span class="attribute">error</span>: <span class="property">@dataRefreshError</span>
            <span class="attribute">data</span>:
                <span class="attribute">url</span>: <span class="property">@sourceUrl</span>()</pre></div>
        
      
        
        <p>When the data has been refreshed successfully, update the <code>data</code> property
which will trigger a change event across the app. If the <code>dataSaveInterval</code>
has passed, it will update the data on the local MongoDB server as well.</p>

        
          <div class='highlight'><pre>    <span class="attribute">dataRefreshSuccess</span>: <span class="function"><span class="params">(resp)</span> =&gt;</span>
        <span class="property">@fetching</span> = <span class="literal">false</span>
        <span class="property">@refreshErrorCount</span> = <span class="number">0</span></pre></div>
        
      
        
        <p>If the data received is not valid, then do nothing.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> <span class="keyword">not</span> resp? <span class="keyword">or</span> _.size(resp) &lt; <span class="number">1</span>
            console.warn <span class="string">"Audit Data JSON is invalid!"</span>, <span class="keyword">this</span>, resp
            <span class="keyword">return</span>

        <span class="property">@data</span> resp
        <span class="property">@lastDataRefresh</span> = <span class="keyword">new</span> Date()

        <span class="keyword">if</span> <span class="property">@lastDataRefresh</span> - <span class="property">@lastDataSave</span> &gt; SystemApp.Settings.auditData.dataSaveInterval
            <span class="property">@save</span>()
            <span class="property">@lastDataSave</span> = <span class="keyword">new</span> Date()

        <span class="property">@trigger</span> <span class="string">"refresh:ok"</span>, <span class="keyword">this</span>, resp</pre></div>
        
      
        
        <p>Trigger an app event with the refresh result. That way we can easily listen
to refresh updates on all views.</p>

        
          <div class='highlight'><pre>        System?.App?.dataEvents.trigger <span class="string">"auditdata:refresh"</span>, <span class="keyword">this</span>, resp</pre></div>
        
      
        
        <p>When the data refresh has thrown an error, trigger an error event which
will display the error info to the user.</p>

        
          <div class='highlight'><pre>    <span class="attribute">dataRefreshError</span>: <span class="function"><span class="params">(resp, status, error)</span> =&gt;</span>
        console.log <span class="string">"Audit Data: REFRESH ERROR"</span>, <span class="keyword">this</span>, error, resp

        <span class="property">@fetching</span> = <span class="literal">false</span>
        <span class="property">@refreshErrorCount</span>++</pre></div>
        
      
        
        <p>If the refresh error count is more than 2 times the value set to show an alert, then
reset it to 0 so the user will see the alert again soon.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> <span class="property">@refreshErrorCount</span> &gt; SystemApp.Settings.auditData.alertOnErrorCount * <span class="number">2</span>
            <span class="property">@refreshErrorCount</span> = <span class="number">0</span>

        <span class="property">@trigger</span> <span class="string">"refresh:error"</span>, <span class="keyword">this</span>, error</pre></div>
        
      
        
        <p>Trigger an app event with the refresh error, so we can easily listen
to refresh errors and alert the user.</p>

        
          <div class='highlight'><pre>        System?.App?.dataEvents.trigger <span class="string">"auditdata:error"</span>, <span class="keyword">this</span>, resp</pre></div>
        
      
        
        <p>Parse the given path and return its result (if path is valid).</p>

        
          <div class='highlight'><pre>    <span class="attribute">getPathValue</span>: <span class="function"><span class="params">(path)</span> =&gt;</span>
        <span class="keyword">return</span> <span class="string">""</span> <span class="keyword">if</span> <span class="keyword">not</span> path? <span class="keyword">or</span> path <span class="keyword">is</span> <span class="string">""</span></pre></div>
        
      
        
        <p>Get the path and first dot or bracket to find the path value.</p>

        
          <div class='highlight'><pre>        firstDot = path.indexOf <span class="string">"."</span>
        firstBracket = path.indexOf <span class="string">"["</span>

        <span class="keyword">if</span> firstDot &gt;= <span class="number">0</span> <span class="keyword">and</span> firstDot &lt; firstBracket
            itemPath = path.substring firstDot
        <span class="keyword">else</span>
            itemPath = path.substring firstBracket

        newValue = jsonPath <span class="property">@data</span>(), <span class="string">"$"</span> + itemPath</pre></div>
        
      
        
        <p>If the value is a number, make sure it&#39;s passed as Float and with 2 decimal cases.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> newValue? <span class="keyword">and</span> <span class="keyword">not</span> isNaN(newValue)
            newValue = parseFloat(newValue[<span class="number">0</span>]).toFixed SystemApp.Settings.auditData.decimalCases

        <span class="keyword">return</span> newValue</pre></div>
        
      
        
        <h2 id="audit-data-collection">AUDIT DATA COLLECTION</h2>

        
      
        
        <p>Represents a collection of audit data models.</p>

        
          <div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">SystemApp</span>.<span class="title">AuditDataCollection</span> <span class="keyword">extends</span> <span class="title">SystemApp</span>.<span class="title">BaseCollection</span></span>

    <span class="attribute">model</span>: SystemApp.AuditData
    <span class="attribute">url</span>: SystemApp.Settings.auditData.url</pre></div>
        
      
      <div class="fleur">h</div>
    </div>
  </div>
</body>
</html>
